//
//  CalenderCollectionViewCell.swift
//  MoodDiary
//
//  Created by MoodDiary on 2020/11/28.
//

import UIKit
import TYCyclePagerView

protocol CalenderCollectionViewCellDelegate: class {
    func calenderCollectionViewCellSelect(select: IndexPath?)
}

class CalenderCollectionViewCell: UICollectionViewCell {

    @IBOutlet weak var imgV: UIImageView!
    
    @IBOutlet weak var label: UILabel!
    
    var dayArray: [DiaryModel]? {
        didSet {
            guard dayArray != nil else {
                cycleView?.isHidden = true
                return
            }
            
            cycleView?.isHidden = false
            
            cycleView?.reloadData()
            
            if dayArray?.count ?? 0 > 1 {
                cycleView?.autoScrollInterval = 3.0
            } else {
                cycleView?.autoScrollInterval = 0
            }
            
        }
    }
    
    var cycleView: TYCyclePagerView?
    
    var delegate: CalenderCollectionViewCellDelegate?
    var currentIndexPath: IndexPath?
    
    
    override func awakeFromNib() {
        super.awakeFromNib()
        
        let cyclePageView = TYCyclePagerView()
        cyclePageView.delegate = self
        cyclePageView.dataSource = self
        cyclePageView.register(UINib(nibName: "CalenderPageCollectionViewCell", bundle: .main), forCellWithReuseIdentifier: "CalenderPageCollectionViewCell")
        addSubview(cyclePageView)
        cycleView = cyclePageView
        
    }
    
    override func layoutSubviews() {
        cycleView?.frame = bounds
    }

}

extension CalenderCollectionViewCell: TYCyclePagerViewDelegate, TYCyclePagerViewDataSource {
    func layout(for pageView: TYCyclePagerView) -> TYCyclePagerViewLayout {
        let layout = TYCyclePagerViewLayout()
        layout.itemSize = bounds.size
        return layout
    }
    
    func numberOfItems(in pageView: TYCyclePagerView) -> Int { dayArray?.count ?? 0 }
    
    func pagerView(_ pagerView: TYCyclePagerView, cellForItemAt index: Int) -> UICollectionViewCell {
        
        let cell: CalenderPageCollectionViewCell = pagerView.dequeueReusableCell(withReuseIdentifier: "CalenderPageCollectionViewCell", for: index) as! CalenderPageCollectionViewCell
        
        if let showImg = UIImage(named: dayArray?[index].moodStr ?? "") {
            cell.imgV.image = showImg
        } else {
            if var imgArray = UserDefaults.standard.array(forKey: KUSERFeelingImagesArrName) as? [[String:Any]] {
                
                if let imgDeletedArray = UserDefaults.standard.array(forKey: KUSERFeelingDeletedImagesArrName) as? [[String:Any]] {
                    
                    imgArray.append(contentsOf: imgDeletedArray)
                }
                
                let imageName = imgArray.first(where: { $0["text"] as! String == dayArray?[index].moodStr ?? "" })?["imgName"] as? String ?? ""
                cell.imgV.image = UIImage(named: imageName)
            }
        }
        return cell
    }
    
    func pagerView(_ pageView: TYCyclePagerView, didSelectedItemCell cell: UICollectionViewCell, at index: Int) {
        delegate?.calenderCollectionViewCellSelect(select: currentIndexPath)
    }
    
}
